<?php	
	//die('script not work');
	error_reporting( 0 );
	
	require_once('config.php');
	include_once('function.php');			
			

	// Create connection
	$con = connectDB($host,$user,$pass,$db);			
	
	// process import
	$products = array();

	// Estimated time to process import
	$time_eta = 0;	

	if(!empty($_POST) && isset($_FILES["file"])) // 
	{		
		// Start time
		$time_start = microtime(true);

		if ( isset($_FILES["file"])) 
		{
			//if there was an error uploading the file
			if ($_FILES["file"]["error"] > 0) 
			{
				echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
				die();
			}

			// move uploaded file
			move_uploaded_file($_FILES["file"]["tmp_name"],  "upload/export.xls");

			set_include_path(get_include_path() . PATH_SEPARATOR . 'includes/Classes/');
			include 'PHPExcel/IOFactory.php';

			// This is the file path to be uploaded.
			$inputFileName = "upload/export.xls"; 

			try {
				$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
			} catch(Exception $e) {
				die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
			}


			$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
			$arrayCount = count($allDataInSheet);  // Here get total count of row in that Excel sheet		
			
			// Remove first array - heading title. 
			array_splice($allDataInSheet, 0, 1);			
			
			$i = 0;
			$insert_id = 0;
			foreach($allDataInSheet as $data)
			{
				$code 	   		= $data['A'];	// mã kh
				$product_code 	= $data['B'];	// mã áo
				$pos 			= $data['C'];	// vi tri
				$product_title  = $data['D'];	// ten ao
				$product_color  = $data['E'];	// mau ao
				$product_size 	= $data['F'];	// size ao				
				
				// insert new record
				if($i == 0)
				{
					$sql = "INSERT INTO `export_form`(`maker`, `maker_code`, `note`, `quantity`, `active`) 
							VALUES ('n/a','$code','n/a','0',0)";				
					$result = mysqli_query($con, $sql);				
					$insert_id = mysqli_insert_id($con);
				}
								
				$sql = "INSERT INTO `code_export_form_tmp`(`export_form_id`, `code`, `active`) 
						VALUES ($insert_id, '$product_code', 0)";
					
				mysqli_query($con, $sql);					

				$i++;
			}			
		}

		// End time
		$time_end = microtime(true);		
		$time_eta = ($time_end - $time_start)/60;

		header("Location: http://shop.aothun.vn/export/aothunvn/exportw/uploadExportForm/".$insert_id);
	}	
		
	
	/*
	echo '<pre>';
	print_r($allDataInSheet);
	echo '</pre>';
	*/	
	
	// import link, export link, setting link
	$main_site = "http://shop.aothun.vn/export/aothunvn/";
	$import_link = "http://shop.aothun.vn/export/aothunvn/importw/";
	$export_link = "http://shop.aothun.vn/export/aothunvn/exportw/";
	$setting_link = "http://shop.aothun.vn/export/aothunvn/importw";	
	//
	$upload_export_site = "http://shop.aothun.vn/export/aothunvn/exportw/uploadExportForm/true";
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8"/>
		 <meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>Import dữ liệu</title>
		
		<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
		<!-- Bootstrap theme -->
		<link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet">		
		<link href="css/theme.css" rel="stylesheet">		
		<script>				
			var local_site = "http://localhost/";				
			
			function submitForm()
			{				
				document.getElementById("my_form").submit();
			}
		</script>
	</head>
	<body role="document">
		 <!-- Fixed navbar -->
		<div class="navbar navbar-default navbar-fixed-top" role="navigation">
		  <div class="container">
			<div class="navbar-header">
			  <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
				<span class="sr-only">Toggle navigation</span>
				<span class="icon-bar"></span>
				<span class="icon-bar"></span>
				<span class="icon-bar"></span>
			  </button>
			  <a class="navbar-brand" href="<?=$main_site; ?>">Aothun.vn</a>
			</div>
			<div class="navbar-collapse collapse">
			  <ul class="nav navbar-nav">
				<li><a href="index.php">Bảng quy đổi</a></li>					
				<li><a href="<?=$import_link; ?>">Nhập kho</a></li>					
				<li><a href="<?=$export_link; ?>">Xuất kho</a></li>							
			  </ul>
			</div><!--/.nav-collapse -->
		  </div>
		</div>
		<!-- End navbar -->
				
		<div class="container theme-showcase" role="main">
			 <div class="page-header">
				<h1>Nhập phiếu xuất kho</h1>
			  </div>

			  <?php if($time_eta > 0): ?>
			  <div class="alert alert-success">
		        Quy trình thực hiện <?=$time_eta ?> phút.
		      </div>
		  	  <?php endif; ?>

			  <p>								
				<button type="button" class="btn btn-lg btn-success" onClick="submitForm()">Import dữ liệu</button>				
			  </p>
			  
			  <!-- Input file .xls to update data -->
			  <form role="form" method="post" enctype="multipart/form-data" id="my_form" action="<?php echo $_SERVER['PHP_SELF'];?>">
				  <div class="form-group">
					<label for="exampleInputFile">Input xls</label>
					<input type="file" id="exampleInputFile" name="file">
					<input type="hidden" name="to_make_it_works">
					<p class="help-block">* Only .xls file is available</p>
				  </div>				  
			  </form>
		</div>
	</body>
</html>